
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>列表</title>
    {include file='header'}
</head>
<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a>首页</a>
            <a>产品管理</a>
        </span>
        <div style="float: right;">            
            <a onclick="xadmin.open('添加','{:url("modify")}')"><button class="layui-btn"><i class="layui-icon">&#xe61f;</i>添加</button></a>
            <!-- <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon">&#xe640;</i>批量删除</button> -->
            <a class="layui-btn layui-btn-small" href="javascript:location.replace(location.href);" title="刷新">
                <i class="layui-icon layui-icon-refresh-1"></i></a>
        </div>
    </div>
    <div class="x-body">
        <div class="layui-row">
            <div class="nav-flex">
            <div class="nav-left">
            <form class="layui-form">
                <div class="layui-input-inline layui-show-xs-block">
                    <input class="layui-input" placeholder="关键词" name="keyword">
                </div>
                <div class="layui-input-inline layui-show-xs-block">
                    <button class="layui-btn"  lay-submit="" lay-filter="sreach">搜索</button>
                </div>
            </form>
            </div>
            <div class="nav-right">{if $pid==2}
                <input type="file" name="file" class="file-file" id="file1" size="28" />
                <input type="button" name="btnup1" class="layui-btn" id="file-btn1" value="导入产品表" />
                <input type="button" name="btnup1" class="layui-btn" id="file-btn2" onclick="exportcsv()" value="导出CSV" />
                {/if}
            </div></div>
        </div>
        <table class="layui-table">
            <thead>
                <tr>
                    <th style="width: 25px;">
                        <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
                    </th>
                    <th style="width: 80px;">分类名称</th>
                    <th>产品名称</th>
                    <th>销售价</th>
                    <th>会员价</th>
                    <th>规格</th>
                    <th>产地</th>
                    <th>库存</th>
                    <th>出库</th>
                    <th>更新时间</th>
                    <th style="width: 100px;text-align: center;">操作</th>
                </tr>
            </thead>
            <tbody>
                {foreach $data_list as $vo}
                <tr>
                    <td style="width: 25px;">
                        <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='{$vo.id}'><i class="layui-icon">&#xe605;</i></div>
                    </td>
                    <td>{if !empty($vo.category)}{$vo.category.name}{/if}</td>
                    <td>{$vo.name}</td>
                    <td>{$vo.price}</td>
                    <td>{$vo.vip_price}</td>
                    
                    <td>{$vo.unit}</td>
                    <td>{$vo.place}</td>
                    <td>{$vo.stock}</td>
                    <td>{$vo.sell_count}</td>
                    <td>{$vo.update_time}</td>
                    <td class="td-manage" style="width:120px;">
                        <a class="layui-btn layui-btn-xs" onclick="xadmin.open('添加','{:url('modify',['id'=>$vo.id])}')">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" onclick="remove({$vo.id})">删 除</a>                        
                    </td>
                </tr>{/foreach}             
            </tbody>
        </table>
        {$page|raw}
    </div>
    <script>
    function exportcsv()
    {
        var param=location.href.split('?')[1];
        window.open('/zongtai/product/exportcsv?'+param)
    }
    function set_status(id,status){
        $.post("{:url('status')}",{id:id,status:status},function(res){
        if(res.code==1)
        {
          layer.msg('操作成功',{icon:1,time:1000},function(){
            location.reload();
          })
        }
        else
        {
          layer.msg('操作失败');
        }
      },'json');
    }
    function remove(id)
    {
        if(confirm('确定要删除数据吗?'))
        {
        $.post("{:url('remove')}",{id:id},function(res){
            if(res.code==1)
            {
            layer.msg('删除成功',{icon:1,time:1000},function(){
                location.reload();
            })
            }
            else
            {
            layer.msg('操作失败');
            }
        })
        }
    }
    /*用户-批量删除*/
    function delAll(argument) {
        var ids = tableCheck.getData().toString();
        if (ids == "") {
            layer.msg('请选择要删除的数据！', { icon: 0 });
            return false;
        }
       
        layer.confirm('确认要删除所选吗？', function (index) {
            layer.close(index);
            //捉到所有被选中的，发异步进行删除
            $.ajax({
                type: "POST",
                url: "{:url('remove')}",
                data: { "ids": ids },
                datatype: "json",
                success: function (res, textStatus) {
                    console.log(res)
                    if (res.code == 1) {
                        layer.msg(res.msg, { icon: 1, time: 1000 },function(){
                            location.reload();
                        });
                        //$(".layui-form-checked").not('.header').parents('tr').remove();
                        
                    }
                    else {
                        layer.alert(res.msg, { icon: 5 });
                        return false;
                    }
                },
                error: function (e) {
                    layer.alert('系统出现错误,请检查系统！', { icon: 2 });//0感叹号  1打勾  2打叉  3问号  4锁头  5难过  6笑脸
                    return false;
                }
            });
        });
    }
    $(function(){
        $("#file-btn1").click(function(){
            $("#file1").click();
        })
        $("#file1").change(function() {
            
            // $.post('/zongtai/import/importexecl',{filepath:'/uploads/shop/20240220/1.xls'},function(result){
            //                   console.log(result)
            //                   layer.msg('更新'+result.data.count+'项产品',{icon:1,time:1000},function(){
            //                         //location.reload()
            //                   });
            //               })
            
            // return
            var formdata = new FormData();
            var fileobj=''
            var fileObj = document.getElementById("file1").files[0];
            formdata.append("file", fileObj);
            $.ajax({
                   url: "{:url('zongtai/upload/upPostfile')}",
                   data: formdata,
                   type: "Post",
                   cache: false,
                   processData: false,
                   contentType: false,
                   success: function (res) {
                        console.log(res);
                       if(res.code==1)
                       {
                           $.post('/zongtai/product/updatebyexcel',{filepath:res.data.filepath},function(result){
                               console.log(result)
                               if(result.code==0)
                               {
                                   layer.msg(result.msg)
                                   return
                               }
                               layer.msg('更新'+result.data.count+'项产品',{icon:1,time:1000},function(){
                                    //location.reload()
                               });
                           })
                       }
                       else
                       {
                           layer.msg(res.msg)
                       }
                   },
                   error:function(){
                       console.log('error')
                   },
                   complete:function()
                   {
                       console.log('end')
                   }
               })
        })
    })
    </script>
</body>
</html>